//
//  MenuViewController.m
//  HandyDialer
//
//	Developed by Team Synergy
//  Created by Jordan Tseng on 10/29/11.
//  Coders: Jacky Li and Jordan Tseng 
//  Graphic Designer: Derek Fong
//  Developers: Brad Lisson, Derek Fong, Jacky Li, Jeremy Gangoso, Jordan Tseng
//  Copyright HandyDialer 2011. All rights reserved.
//


#import "MenuViewController.h"


@implementation MenuViewController
@synthesize  highlightArea;
@synthesize backgroundArea;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}



#pragma mark - View lifecycle

- (void)dealloc
{
	[highlightArea release];
    [super dealloc];
}


- (void)viewDidLoad 
{
	
    counter = 0;
	defaults = [NSUserDefaults standardUserDefaults];
	speed = [defaults floatForKey:@"Speed"];
	highlightColor = [defaults stringForKey:@"Highlight"];
	themeColor = [defaults stringForKey:@"Theme"];
	[backgroundArea setImage:[UIImage imageNamed: [themeColor stringByAppendingString:@"_menu_default.png"]]];
	[super viewDidLoad];
    
}

- (void)viewDidAppear:(BOOL)animated
{
	defaults = [NSUserDefaults standardUserDefaults];
    speed = [defaults floatForKey:@"Speed"];
	highlightColor = [defaults stringForKey:@"Highlight"];
	themeColor = [defaults stringForKey:@"Theme"];
    [backgroundArea setImage:[UIImage imageNamed: [themeColor stringByAppendingString:@"_menu_default.png"]]];
	
	if (uniTimer == nil)
		uniTimer = [NSTimer scheduledTimerWithTimeInterval:(speed) target:self selector:@selector(optionChange) userInfo:nil repeats:YES];
	[super viewDidAppear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    // Stops timer if view is no longer in front or app is minimized
	[uniTimer invalidate];
	uniTimer = nil;
	[super viewDidDisappear:animated];
}



// Actually switching to that screen 
// 1. DialerViewController
// 2. Directory
- (IBAction)switchView:(id)sender
{
	if(counter == 1)
	{
		counter = 0;
		DialerViewController	*myViewController = [[DialerViewController alloc]
										 initWithNibName:@"DialerView"
										 bundle:nil];

		[self presentModalViewController:myViewController animated:NO];
		;
		[highlightArea setImage:nil];
	}
	else if(counter == 2)
	{
		counter = 0;
		SearchDirectoryViewController	*myViewController = [[SearchDirectoryViewController alloc]
										 initWithNibName:@"SearchDirectoryView"
										 bundle:nil];
		
		[self presentModalViewController:myViewController animated:NO];
		;
		[highlightArea setImage:nil];
	}
    else if(counter == 3)
    {
        counter = 0;
        CallLogViewController *myViewController = [[CallLogViewController alloc]
                                                   initWithNibName:@"CallLogView"
                                                   bundle:nil];
        
        [self presentModalViewController:myViewController animated:NO];
		;
		[highlightArea setImage:nil];
    }
    else if(counter == 4)
    {
        counter = 0;
        ContactViewController *myViewController = [[ContactViewController alloc]
                                                   initWithNibName:@"ContactView"
                                                   bundle:nil];
        
        [self presentModalViewController:myViewController animated:NO];
		;
		[highlightArea setImage:nil];

    }
    else if(counter == 5)
    {
        counter = 0;
        SettingViewController *myViewController = [[SettingViewController alloc]
                                                   initWithNibName:@"SettingView"
                                                   bundle:nil];
        
        [self presentModalViewController:myViewController animated:NO];
		;
		[highlightArea setImage:nil];
    }
}



- (void)highlightChange:(NSString *)picture
{
    UIImage *image = [UIImage imageNamed: picture];
    [highlightArea setImage:image];
	
}

// Decides which screen to go to
-(void)optionChange 
{
    
    if (counter == 5)
    {
        counter = 0;
    }
    counter += 1;
    NSString *next = highlightColor;
	next = [next stringByAppendingString:@"_menu_"];
	next = [next stringByAppendingString:[NSString stringWithFormat:@"%d", counter]];
	next = [next stringByAppendingString:@".png"];
	[self highlightChange:next];
}
@end
